﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class LogInfo
    {
        #region Properties
        public User User
        {
            get
            {
                return this.GetUser();
            }
        }

        public Info Info
        {
            get
            {
                return this.GetInfo();
            }
        }
        public Action Action
        {
            get
            {
                return this.GetAction();
            }
        }
        #endregion

        #region Methods
        public User GetUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Users.Where(p => p.ID == this.ID_User);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Info GetInfo()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Infos.Where(p => p.ID == this.ID_Info);
            if (v.Count() == 0)
                return null;
            return v.First();
        }
        public Action GetAction()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Actions.Where(p => p.ID == this.ID_Action);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<ErrorItem> Rollback(int id_User, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            String[] lContent = this.Content.Split('_');

            int errorMess = 0;
            try
            {

                if (lContent[0].Contains("[Detail]"))
                {
                    DetailInfo di = new DetailInfo();
                    di.ID_Info = this.ID_Info;
                    di.ID_Language = int.Parse(lContent[1]);
                    di.CompanyName = lContent[2];
                    di.Address = lContent[3];
                    di.Description = lContent[4];

                    errorList = DetailInfo.ChangeInformation(di, id_User, this.ID, id_Lang);
                }
                else
                {
                    Info i = new Models.Info();
                    i.ID = this.ID_Info;
                    i.Tel = lContent[0];
                    i.Mobile = lContent[1];
                    i.ID_Status = int.Parse(lContent[2]);

                    if (this.Source == null)
                        errorList = Info.ChangeInformation(i, id_User, this.ID, id_Lang, 0);
                    else
                        errorList = Info.ChangeInformation(i, id_User, this.ID, id_Lang, 1);
                }
            }
            catch
            {
                errorMess = 48;
            }

            if (errorMess != 0)
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));

            return errorList;
        }
        #endregion

        #region Static Methods
        public static List<LogInfo> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            return _db.LogInfos.OrderByDescending(p => p.ActionTime).ToList();
        }
        #endregion
    }
}